
function ShoppingCart(){
}
ShoppingCart.addProduct=function(product,user){
    var goodsInfo=new AV.Object('GoodsInfo');
    goodsInfo.set('classification',product.classification);
    goodsInfo.set('itemName',product.itemName);
    goodsInfo.set('price',product.price);
    goodsInfo.set('unit',product.unit);
    goodsInfo.set('count',1);
    goodsInfo.set('promotionCount',0);
    goodsInfo.set('savePrice',0);
    goodsInfo.set('costPrice',product.price);
    goodsInfo.set('actuallyPaid',product.price);
    AV.Object.saveAll(goodsInfo).then(function(info){
        var relation=user.relation('productInfo');
        relation.add(info);
        user.save();
    },function(error){
        alert("错误!")
    })
};
ShoppingCart.computeActuallyPaid=function(itemInfo){
    var itemCount=itemInfo.get('count')-itemInfo.get('promotionCount');
    var  actuallyPaid=itemInfo.get('price')*itemCount;
    itemInfo.set('actuallyPaid',actuallyPaid);
    return  itemInfo
};
ShoppingCart.computeSumActuallyPaidPrice=function(data){
    var sumActuallyPaid=0;
    for(var i=0;i<data.length;i++){
        sumActuallyPaid+=parseFloat(data[i].actuallyPaid||0);
    }
    return  sumActuallyPaid.toFixed(2)
};
ShoppingCart.computeSumSavePrice=function(data){
     var   sumSavePrice=0;
    for(var i=0;i<data.length;i++){
        sumSavePrice+=parseFloat( data[i].savePrice||0);
    }
    return sumSavePrice.toFixed(2);
}
ShoppingCart.computeSumCount=function(results){
    var sumCount=0;
    for(var i=0;i<results.length;i++){
        sumCount+=(results[i].get('count')||0);
    }
    return sumCount;
};
ShoppingCart.computeSumPrice=function(data){
    var sumPrice=0;
    for(var i=0;i<data.length;i++){
        sumPrice+=parseFloat(data[i].costPrice||0);
    }
    return sumPrice.toFixed(2)
};


